package org.davidliebman.android.flyer;


import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

public  class Panel  implements  GLSurfaceView.Renderer {
	private GameValues mGameV;
	private Canvas mCanvas;

	private MovementValues mMovementV;
	private Record mHighScores;
	private int mDisplayViewWidth;
	private Matrix mMatrix;
	
	private int scrollX, scrollY;
	private Bitmap  mMap, mTempJNI;
	private BitmapFactory.Options mOptionsSprite = new BitmapFactory.Options();
	private BitmapFactory.Options mOptionsTile = new BitmapFactory.Options();
	private BitmapFactory.Options mOptionsNum = new BitmapFactory.Options();
	private BitmapFactory.Options mOptionsPlat = new BitmapFactory.Options();
	private BitmapFactory.Options mOptionsFlyer = new BitmapFactory.Options();
	private BitmapFactory.Options mOptionsExplosion = new BitmapFactory.Options();

	
	private SpriteInfo mGuySprite;
	private Paint mP;
	private float mScaleH = 2;
	private float mScaleV = 2;

	public static final int DRIFT_NONE = 0;
	public static final int DRIFT_LEFT = 1;
	public static final int DRIFT_RIGHT = 2;
	public static final int DRIFT_CENTER_FROM_LEFT = 3;
	public static final int DRIFT_CENTER_FROM_RIGHT = 4;
	public static final int DRIFT_DOWN = 5;
	private int mDrift;
	
	/* -- start 'for scrolling' -- */
	/* for scrolling */
	private final int LR_MARGIN = 80;
	private final int TB_MARGIN = 40;

	/* for scrolling, collision, etc. */
	private boolean canScroll;
	private int oldX;
	private int oldY;
	private int screenX;
	private int screenY;
	private int mapH;
	private int mapV;
	
	private boolean mCanSkip; // for problem spots
	
	private int mScreenW;
	//private int mScreenH;
	
	private int mapX;
	private int mapY;

	private int newMapX;
	private int newMapY;

	private int newX;
	private int newY;

	private int guyWidth; 
	private int guyHeight;

	private int x;
	private int y;

	private boolean keyB = false;
	private int jumptime;

	private double mXMultiplier = 1;
	private double mYMultiplier = 1;

	private boolean ladderTest = false;
	private boolean blockTest = false;
	private boolean boundaryTest = false;
	private boolean boundaryLeft = false;
	private boolean boundaryRight = false;
	private boolean canFall = false;
	private boolean canJump = false;
	
	public static final int END = 1;
	public static final int MIDDLE = 2;
	public static final int START = 3;

	public static final int JNI_TRUE = 1;
	public static final int JNI_FALSE = 0;
	
	/* -- end 'for scrolling' -- */

	/* for monster animation */
	public static final int ANIM_SPEED = 5;


	/* animation vars */
	private static int ANIMATE_SPEED = 0;
	private int animate, newGuy, newBG, lastGuy, lastBG;

	/* test jni */
	//int [] screen = new int [192 * 256];
	SoundPoolManager mSounds;
	private boolean mEnableSounds;
	private boolean mAnimationOnly;
	//public static int mSDKVersion;
	
	public Panel(Context context,  GameValues gameValues, GameStart parent, MovementValues movementValues) {
		
		//mSDKVersion = new Integer(android.os.Build.VERSION.SDK).intValue();
		this.JNIsetVersion(0);
		
		mGameV = gameValues;
		mMovementV = movementValues;

		mGameV.setSpriteStart();
		mGuySprite = mGameV.getSpriteStart();

		
		
		/* display width considerations */
    	Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();    	
    	int displayWidth = display.getWidth();
    	int displayHeight = display.getHeight();
		
    	if(mGameV.getScreenOrientation() == GameValues.ORIENTATION_PORTRAIT) {
	    	
			if ( displayWidth < 256 * 2 ) {
				mDisplayViewWidth = mGameV.getDisplayWidth();//displayWidth;
				mScaleH = mGameV.getScaleH();
			}
			else {
				mDisplayViewWidth = 256 * 2;
			}
			
			if (!mGameV.isDoubleScreen()) {
				mScaleH = mGameV.getScaleH();
				mScaleV = 1;

			}
			
    	}
    	else if(mGameV.getScreenOrientation() == GameValues.ORIENTATION_LANDSCAPE) {
			mScaleH = mGameV.getScaleH();
			mScaleV = mGameV.getScaleV();
			mDisplayViewWidth = 256;
		}
		
		/* paint options BitmapFactory.Options */
    	mOptionsExplosion.inScaled = false;
    	mOptionsExplosion.outHeight = 64;
    	mOptionsExplosion.outWidth = 64;
    	mOptionsExplosion.inPreferredConfig = Bitmap.Config.ARGB_4444;
    	
    	mOptionsFlyer.inScaled = false;
    	mOptionsFlyer.outHeight = 16;
    	mOptionsFlyer.outWidth = 40;
    	mOptionsFlyer.inPreferredConfig = Bitmap.Config.ARGB_4444;
    	
		mOptionsSprite.inScaled = false;
		mOptionsSprite.outHeight = 16;
		mOptionsSprite.outWidth = 16;
		mOptionsSprite.inPreferredConfig = Bitmap.Config.ARGB_4444;
		
		mOptionsTile.inScaled = false;
		mOptionsTile.outHeight = 128;
		mOptionsTile.outWidth = 224;
		mOptionsTile.inDensity = 0;
		mOptionsTile.inTargetDensity = 0;
		mOptionsTile.inPreferredConfig = Bitmap.Config.ARGB_4444;
		
		mOptionsNum.inScaled = false;
		mOptionsNum.outHeight = 16;//16
		mOptionsNum.outWidth = 160;// 160
		mOptionsNum.inDensity = 0;//0
		mOptionsNum.inTargetDensity = 0;//0
		mOptionsNum.inPreferredConfig = Bitmap.Config.ARGB_4444;
		
		mOptionsPlat.inScaled = false;
		mOptionsPlat.outHeight = 16;//16
		mOptionsPlat.outWidth = 160;// 160
		mOptionsPlat.inDensity = 0;//0
		mOptionsPlat.inTargetDensity = 0;//0
		mOptionsPlat.inPreferredConfig = Bitmap.Config.ARGB_4444;
		
		mP = new Paint();
		mP.setAlpha(0xff);
		mMatrix = new Matrix();
		mMatrix.setScale(mScaleH, mScaleV);

		scrollX = mMovementV.getScrollX();
		scrollY = mMovementV.getScrollY();
		
		/*animation vars*/
		animate = 0;
		newGuy = 0;
		lastGuy = 0;
		newBG = 0;
		lastBG = 0;

		mAnimationOnly = false;
		
		mHighScores = mGameV.getScores().getHighScorePrefs();//mGameV.getGuyScore();
		mSounds = new SoundPoolManager(parent);
		mSounds.init();
		
		/* prepare jni -- load all images into library */
		int [] a = new int[16*40];
		int [] b = new int[16*40];
		int [] c = new int[16*40];
		int [] d = new int[16*40];

		Bitmap mFlyer;
		mFlyer = BitmapFactory.decodeResource(context.getResources(),R.drawable.flyer_l0 , mOptionsFlyer);
		mFlyer.getPixels(a, 0, 40, 0, 0, 40, 16);
		mFlyer = BitmapFactory.decodeResource(context.getResources(),R.drawable.flyer_l1 , mOptionsFlyer);
		mFlyer.getPixels(b, 0, 40, 0, 0, 40, 16);
		mFlyer = BitmapFactory.decodeResource(context.getResources(),R.drawable.flyer_r0 , mOptionsFlyer);
		mFlyer.getPixels(c, 0, 40, 0, 0, 40, 16);
		mFlyer = BitmapFactory.decodeResource(context.getResources(),R.drawable.flyer_r1 , mOptionsFlyer);
		mFlyer.getPixels(d, 0, 40, 0, 0, 40, 16);
		setFlyerData(a, b, c, d);
		
		a = new int[16*16];
		b = new int[16*16];
		c = new int[16*16];
		d = new int[16*16];
		
		Bitmap mSprite;
		mSprite = BitmapFactory.decodeResource(context.getResources(),R.drawable.guy0, mOptionsSprite);
		mSprite.getPixels(a, 0, 16, 0, 0, 16, 16);
		mSprite = BitmapFactory.decodeResource(context.getResources(),R.drawable.guy1, mOptionsSprite);
		mSprite.getPixels(b, 0, 16, 0, 0, 16, 16);
		mSprite = BitmapFactory.decodeResource(context.getResources(),R.drawable.guy2, mOptionsSprite);
		mSprite.getPixels(c, 0, 16, 0, 0, 16, 16);
		mSprite = BitmapFactory.decodeResource(context.getResources(),R.drawable.guy3, mOptionsSprite);
		mSprite.getPixels(d, 0, 16, 0, 0, 16, 16);
		setGuyData(a, b, c, d);
		
		mSprite = BitmapFactory.decodeResource(context.getResources(),R.drawable.monster_r0, mOptionsSprite);
		mSprite.getPixels(a, 0, 16, 0, 0, 16, 16);
		mSprite = BitmapFactory.decodeResource(context.getResources(),R.drawable.monster_r1, mOptionsSprite);
		mSprite.getPixels(b, 0, 16, 0, 0, 16, 16);
		mSprite = BitmapFactory.decodeResource(context.getResources(),R.drawable.monster_l0, mOptionsSprite);
		mSprite.getPixels(c, 0, 16, 0, 0, 16, 16);
		mSprite = BitmapFactory.decodeResource(context.getResources(),R.drawable.monster_l1, mOptionsSprite);
		mSprite.getPixels(d, 0, 16, 0, 0, 16, 16);
		setMonsterData(a, b, c, d);

		mSprite = BitmapFactory.decodeResource(context.getResources(), R.drawable.inv1_l, mOptionsSprite);
		mSprite.getPixels(a, 0, 16, 0, 0, 16, 16);
		mSprite = BitmapFactory.decodeResource(context.getResources(), R.drawable.inv1_r, mOptionsSprite);
		mSprite.getPixels(b, 0, 16, 0, 0, 16, 16);
		setInvader1Data(a,b);
		
		a = new int [8 * 8];
		b = new int [8 * 8];
		
		mSprite = BitmapFactory.decodeResource(context.getResources(), R.drawable.rollee_a, mOptionsSprite);
		mSprite.getPixels(a, 0, 8, 0, 0, 8, 8);
		mSprite = BitmapFactory.decodeResource(context.getResources(), R.drawable.rollee_b, mOptionsSprite);
		mSprite.getPixels(b, 0, 8, 0, 0, 8, 8);
		setInvader2Data(a,b);
		
		int [] tiles_a = new int [128 * 224];
		int [] tiles_b = new int [128 * 224];
		int [] tiles_c = new int [128 * 224];
		int [] tiles_d = new int [128 * 224];

		Bitmap mTiles;
		mTiles = BitmapFactory.decodeResource(context.getResources(), R.raw.tiles1, mOptionsTile);
		mTiles.getPixels(tiles_a, 0, 224, 0, 0, 224, 128);
		mTiles = BitmapFactory.decodeResource(context.getResources(), R.raw.tiles2, mOptionsTile);
		mTiles.getPixels(tiles_b, 0, 224, 0, 0, 224, 128);
		mTiles = BitmapFactory.decodeResource(context.getResources(), R.raw.tiles3, mOptionsTile);
		mTiles.getPixels(tiles_c, 0, 224, 0, 0, 224, 128);
		mTiles = BitmapFactory.decodeResource(context.getResources(), R.raw.tiles4, mOptionsTile);
		mTiles.getPixels(tiles_d, 0, 224, 0, 0, 224, 128);
		this.setTileMapData(tiles_a, tiles_b, tiles_c, tiles_d);
		
		int [] platform_a = new int [8 * 40];
		Bitmap mPlatform;
		mPlatform = BitmapFactory.decodeResource(context.getResources(), R.drawable.concrete, mOptionsPlat);
		mPlatform.getPixels(platform_a, 0, 40, 0, 0, 40, 8);
		this.setMovingPlatformData(platform_a);
		
		a = new int[64*64];
		b = new int[64*64];
		c = new int[64*64];
		d = new int[64*64];
		int [] e = new int[64*64];
		int [] f = new int[64*64];
		int [] g = new int[64*64];
		int [] h = new int[64*64];
		
		Bitmap mExplosion;
		mExplosion = BitmapFactory.decodeResource(context.getResources(),R.drawable.explosion_a, mOptionsExplosion);
		mExplosion.getPixels(a, 0, 64, 0, 0, 64, 64);
		mExplosion = BitmapFactory.decodeResource(context.getResources(),R.drawable.explosion_b, mOptionsExplosion);
		mExplosion.getPixels(b, 0, 64, 0, 0, 64, 64);
		mExplosion = BitmapFactory.decodeResource(context.getResources(),R.drawable.explosion_c, mOptionsExplosion);
		mExplosion.getPixels(c, 0, 64, 0, 0, 64, 64);
		mExplosion = BitmapFactory.decodeResource(context.getResources(),R.drawable.explosion_d, mOptionsExplosion);
		mExplosion.getPixels(d, 0, 64, 0, 0, 64, 64);
		mExplosion = BitmapFactory.decodeResource(context.getResources(),R.drawable.explosion_e, mOptionsExplosion);
		mExplosion.getPixels(e, 0, 64, 0, 0, 64, 64);
		mExplosion = BitmapFactory.decodeResource(context.getResources(),R.drawable.explosion_f, mOptionsExplosion);
		mExplosion.getPixels(f, 0, 64, 0, 0, 64, 64);
		mExplosion = BitmapFactory.decodeResource(context.getResources(),R.drawable.explosion_g, mOptionsExplosion);
		mExplosion.getPixels(g, 0, 64, 0, 0, 64, 64);
		mExplosion = BitmapFactory.decodeResource(context.getResources(),R.drawable.explosion_h, mOptionsExplosion);
		mExplosion.getPixels(h, 0, 64, 0, 0, 64, 64);
		setExplosionData(a, b, c, d, e, f, g, h);
		
//		boolean testexplosion = false;
//		for(int z = 0; z < 64 * 64; z ++) {
//			if (a[z] != 0) {
//				testexplosion = true;
//			}
//		}
//		if (  testexplosion ) Log.e("tag", "content");
		/* JNI display size setting */
		setScreenData(mGameV.getScreenTilesHMod(), 24);
		
		/* JNI Monster Collision setting */
		int monsters = JNI_FALSE;
		int collision = JNI_FALSE;
		if(mHighScores.isEnableMonsters()) monsters = JNI_TRUE;
		if(mHighScores.isEnableCollision()) collision = JNI_TRUE;
		setMonsterPreferences(monsters, collision);
		
		this.callJNIdrawLevel();
	}


	public void callJNIdrawLevel() {
		//drawLevel();
		this.JNIbuildLevel();
	}
	
	public void setInitialBackgroundGraphics() {
		mHighScores = mGameV.getScores().getHighScorePrefs();//mGameV.getGuyScore();
		
		
		/*** set initial scroll positions ***/ 
		scrollX = mMovementV.getScrollX();
		scrollY = mMovementV.getScrollY();

		/*** Load sprites for level ***/
		mGameV.setSpriteStart();
		mGuySprite = mGameV.getSpriteStart();

		/* JNI Monster Collision setting */
		int monsters = JNI_FALSE;
		int collision = JNI_FALSE;
		if(mHighScores.isEnableMonsters()) monsters = JNI_TRUE;
		if(mHighScores.isEnableCollision()) collision = JNI_TRUE;
		setMonsterPreferences(monsters, collision);
	}

	public void setReturnBackgroundGraphics() {
		mHighScores = mGameV.getScores().getHighScorePrefs();//mGameV.getGuyScore();
		
		/*** jni lives and score ***/
		setScoreLives(mGameV.getScore(), mGameV.getLives());
		mHighScores.setLives(mGameV.getLives());
		mHighScores.setScore(mGameV.getScore());
		
		/*** set initial scroll positions ***/
		scrollX = mGameV.getScrollX();
		scrollY = mGameV.getScrollY();
		setJNIScroll(scrollX, scrollY);
		
		/*** Load sprites for level ***/
		mGuySprite = mGameV.getSpriteStart();

		/*** JNI Monster Collision setting ***/
		int monsters = JNI_FALSE;
		int collision = JNI_FALSE;
		if(mHighScores.isEnableMonsters()) monsters = JNI_TRUE;
		if(mHighScores.isEnableCollision()) collision = JNI_TRUE;
		setMonsterPreferences(monsters, collision);
	}


	public void setGameValues(GameValues g) {
		mGameV = g;

	}
	public GameValues getGameValues() {
		return mGameV;
	}
	
	public void setPanelScroll(int x, int y) {
		scrollX = x;
		scrollY = y;
		mGuySprite = mGameV.getSpriteStart();
		int mGuyX = mGuySprite.getMapPosX();
		int mGuyY = mGuySprite.getMapPosY();
		
		int local_keyB = Panel.JNI_FALSE;
		if (keyB) local_keyB = Panel.JNI_TRUE;
		
		setGuyPosition(mGuyX  , mGuyY , scrollX, scrollY, local_keyB);
		
	}


	


	public void scrollBg() {
		/* scroll registers for background */

		if(JNIisAnimateOnly() == Panel.JNI_TRUE) {
			mAnimationOnly = true;
			x = 0;
			y = 0;
		}
		else {
			mAnimationOnly = false;
		}
		mDrift = Panel.DRIFT_NONE;
		
		canScroll = true;
		oldX = mMovementV.getScrollX();
		oldY = mMovementV.getScrollY();
		screenX = oldX;
		screenY = oldY;
		mapH = mGameV.getMapH();
		mapV = mGameV.getMapV();

		mapX = mGuySprite.getMapPosX();
		mapY = mGuySprite.getMapPosY();

		newMapX = mapX;
		newMapY = mapY;

		//newX = mGuySprite.getX();
		//newY = mGuySprite.getY();

		guyWidth = (mGuySprite.getRightBB() - mGuySprite.getLeftBB()) + 5; // 12 ?
		guyHeight = mGuySprite.getBottomBB() - mGuySprite.getTopBB();

		int tilesMeasurement;
		int screenInTilesVert = 20;

		if (mGameV.isDoubleScreen()) {
			tilesMeasurement = ((this.mDisplayViewWidth / 2 ) / 8) ;
			if (tilesMeasurement > 32 ) tilesMeasurement = 32;
			this.mScreenW = tilesMeasurement * 8;
			//if (tilesMeasurement * 16 < this.mDisplayWidth) tilesMeasurement ++;

		}
		else {
			this.mScreenW = this.mDisplayViewWidth;
			tilesMeasurement = 32;
			//tilesMeasurement = this.mDisplayViewWidth /8 ;
		}
		
		if (isLanding() && y > 0) {
			y = - y;
			
		}
		
		/* 
		 * determine position of guy on screen and determine position
		 * of background on screen also... set scrolling, etc. x and y
		 * are set by routine 'readKeys()'
		 */

		if (x > 0) {   // RIGHT
			
			if (screenX > newMapX &&
					(GameValues.MAP_TILES_H * 8) - screenX + newMapX +x >   LR_MARGIN + mGuySprite.getLeftBB()){
				mDrift = Panel.DRIFT_LEFT;
				//Log.e("Panel", "drift left " +newMapX + "," +screenX +","+ mScreenW);
			}
			
			if (newMapX +x >  LR_MARGIN + mGuySprite.getLeftBB()  && screenX > (GameValues.MAP_TILES_H * 8) - newMapX) {
				mDrift = Panel.DRIFT_LEFT;
				//Log.e("Panel","second drift left");
			}


			
			this.adjust_x_going_right();
			////////////////
//			if (newMapX + x >= mapH * 8 ) {
//				newMapX =  newMapX - (mapH * 8) ;
//				//screenX = screenX - (mapH * 8) ;
//			}
			//newX += x;
			//newMapX += x;
			
			screenX += x;
			newMapX += x;
			
			mMovementV.incrementHCounter();
			
		}  



		//////////////////////////////////////
		else if (x < 0) { //LEFT
			if (newMapX +x <  ((screenX + mScreenW) -  LR_MARGIN ) - mGuySprite.getRightBB() ){
				mDrift = Panel.DRIFT_RIGHT;
				//Log.e("Panel", "drift right");
			}
			if (newMapX + x < LR_MARGIN - mGuySprite.getRightBB() && screenX > newMapX) {
				mDrift = Panel.DRIFT_RIGHT;
				//Log.e("Panel","second drift right");
			}


			
			this.adjust_x_going_left();
			///////////////////////
//			if(newMapX +x <= 0 ) {
//				newMapX =  (mapH * 8);
//				//screenX =  (screenX + (mapH * 8)) ;
//			}
			//newX += x;
			//newMapX += x;
			
			screenX += x;
			newMapX += x;
			
			mMovementV.incrementHCounter();


		}  



		//////////////////////////////////////
		if (y > 0) {   
			if (oldY > mapV * 8) oldY = -1; 

			if (oldY >= ((mapV - screenInTilesVert) * 8 - y) ) canScroll = false;
			else canScroll = true;
			//move DOWN?
			if (mapY + y >= mapV * 8  - guyHeight) {
				newMapY = mapV * 8  - guyHeight;
				newY = 24 * 8 - guyHeight;

			}

			if ((mapY + y) >= (oldY + TB_MARGIN) ) {        


				if (canScroll) {
					screenY += y;
					newMapY += y;
				}
				else if ( mapY <= mapV * 8  - guyHeight ) {
					newY += y;
					newMapY += y;
				}

			}
			else if ((mapY + y) <= (oldY + TB_MARGIN) &&  canScroll) {
				//move sprite?
				newY += y;
				newMapY += y;

			}

		}  
		////////////////////////////////////// 
		else if (y < 0) {   
			if (oldY > mapV * 8) oldY = -1;

			if (oldY < ( 0 - y) ) canScroll = false;
			else canScroll = true;
			//move UP?
			if ( mapY + y <= 0) {
				newMapY = 1;
				newY = 1;

			}

			if ((mapY + y) <= (oldY +( (screenInTilesVert ) * 8 ) - TB_MARGIN) ) { //32 * 8       


				if (canScroll && screenY + y > 0) {
					screenY += y;
					newMapY += y;

				}
				else if ( mapY >= 0 ) {
					newY += y;
					newMapY += y;
				}

			}
			else if ((mapY + y) >= (oldY + ( screenInTilesVert  * 8 ) - TB_MARGIN) &&  canScroll) { //32 * 8
				//move sprite?
				newY += y;
				newMapY += y;

			}

		}
		////////////////////////
		//special movement for drift
		boolean mShift = false;
		
		switch( mDrift ) {
		case Panel.DRIFT_LEFT:
			
			if (x != 0) {
				if (screenX < newMapX && newMapX +x >  screenX + LR_MARGIN + mGuySprite.getLeftBB() ) {
					//newMapX = (newMapX - Math.abs(x));
					mShift = true;
					//Log.e("Panel", "move left");
				}
				int mLeftMargin = LR_MARGIN + mGuySprite.getLeftBB();
				if (screenX > newMapX && 
						(GameValues.MAP_TILES_H * 8) - screenX + newMapX + x > mLeftMargin) {
					mShift = true;
					//newMapX = (newMapX - Math.abs(x));
					//Log.e("Panel","move left again");
				}

				if (mShift) {
					newMapX = newMapX - Math.abs(x);
				}
				this.adjust_x_going_left();
			}
			break;
		case Panel.DRIFT_RIGHT:
			
			if (x != 0) {
				if (screenX < newMapX &&  newMapX +x <  ((screenX + mScreenW) -  LR_MARGIN ) - mGuySprite.getRightBB() ) {
					//newMapX = (newMapX + Math.abs(x));
					mShift = true;
					//Log.e("Panel", "move right");
				}
				int mRightMargin = LR_MARGIN + mGuySprite.getRightBB();
				if ((  screenX > newMapX && 
						(GameValues.MAP_TILES_H * 8) - screenX + newMapX + x <  (mGameV.getScreenTilesHMod() * 8) - mRightMargin )  ) {
					mShift = true;
					//newMapX = (newMapX + Math.abs(x));
					//Log.e("Panel", "screentilesmodH " + mGameV.getScreenTilesHMod() * 8);
				}
				
				if (mShift) {
					newMapX = newMapX + Math.abs(x);
				}
				this.adjust_x_going_right();
			}
			break;
		case Panel.DRIFT_DOWN:
			break;
		case Panel.DRIFT_CENTER_FROM_LEFT:
			break;
		case Panel.DRIFT_CENTER_FROM_RIGHT:
			break;
		}
		
		
		//mDrift = Panel.DRIFT_NONE;
		
		////////////////////////
		
		mGuySprite.setMapPosX(newMapX);
		mGuySprite.setMapPosY(newMapY);
		
		mMovementV.setScrollX(screenX);
		mMovementV.setScrollY(screenY);
	}
	
	private void adjust_x_going_right( ) {
		
		if (screenX + x >= mapH * 8  ) {
			newMapX = newMapX - screenX;
			if (newMapX < 0 ) {
				int mChange = Math.abs(newMapX);
				//Log.e("Panel","x under right " + newMapX);
				newMapX = (mapH * 8) - mChange;
				

			}
			screenX = 0;
		}
		if (newMapX + x >= mapH * 8 ) {
			newMapX = 0;
			//screenX = screenX - (mapH * 8);
		}


	}
	private void adjust_x_going_left( ) {
		
		if (screenX + x <= 0  ) {
			

			screenX = mapH * 8;
		}
		if ( newMapX + x <=0 ) {
			newMapX =  (mapH * 8) ;
			//screenX = screenX - (mapH * 8);
			//Log.e("Panel"," second screenX change");
		}


	}
	public void checkRegularCollisions() {

		/*
		 * Here we create a BoundingBox for the guy character. Then
		 * we check the level for collisions. The object is to record when the 
		 * character comes into contact with various objects.
		 */


		

	}
	
	
	public void setKeyB(boolean b) {
		keyB = b;
	}



	public boolean isAnimationOnly() {
		return mAnimationOnly;
	}


	public void setAnimationOnly(boolean mAnimationOnly) {
		this.mAnimationOnly = mAnimationOnly;
	}


	/**  used to refresh reference to Guy Sprite before start of level. **/
	public void setGuySprite(SpriteInfo sprite) {
		mGuySprite = sprite;
	}
	
	

	
	public Record getHighScores() {
		return mHighScores;
	}


	public void setHighScores(Record mHighScores) {
		this.mHighScores = mHighScores;
	}
	

	public boolean isEnableSounds() {
		return mEnableSounds;
	}


	public void setEnableSounds(boolean mEnableSounds) {
		this.mEnableSounds = mEnableSounds;
		mSounds.setEnabled(mEnableSounds);
	}


	public   void readKeys(double num) {
		mXMultiplier = num;
		mYMultiplier = num;
		readKeys();
		mXMultiplier = 1;
		mYMultiplier = 1;
	}


	public  void readKeys() {		


		/* set x and y as determined by game pad input */
		x=0;
		y=0;

		//keyB = false;

		//changeX = false;
		//changeY = false;

		if(mMovementV.getDirectionLR() == MovementValues.KEY_RIGHT) {

			x =  (int) + (mMovementV.getHMove() * mXMultiplier);
			//changeX = true;
			//keyB = false;
		}
		if(mMovementV.getDirectionLR() == MovementValues.KEY_LEFT) {
			x =   (int) - (mMovementV.getHMove() * mXMultiplier);
			//changeX = true;
			//keyB = false;
		}
		if(mMovementV.getDirectionUD() == MovementValues.KEY_UP) {
			y =  (int) - (mMovementV.getVMove() * mYMultiplier);
			//changeY = true;
			//keyB = false;
		}
		if(mMovementV.getDirectionUD() == MovementValues.KEY_DOWN) {
			y =  (int) + (mMovementV.getVMove() * mYMultiplier);
			//changeY = true;
			//keyB = false;
		}
		

	}
	
	public int pointToBlockNum(int x, int y) {
		int mNewX, mNewY;
		mNewX = x / 8;
		mNewY = y / 8;
		return mGameV.getObjectsCell(mNewX, mNewY);
	}
	
	@Override
	public void onDrawFrame(GL10 gl) {

		
		this.JNIdraw();
		//Log.e("tag","score " + this.JNItestScore());
	}


	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {

		this.JNIresize(width, height);

	}


	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {

		this.JNIinit();
		this.JNIblankScreen(Panel.JNI_TRUE);


		mGameV.getHandler().sendEmptyMessage(GameStart.STARTLEVEL);

		
	}




	/* query the jni for sprite info used for Bundle */
	public void updateSpriteList() {
		for (int x = 0; x < mGameV.getSpriteListSize() - 1; x ++) {
			if(this.getSpriteFacingRight(x) == 1) mGameV.getSprite(x + 1).setFacingRight(true);
			else mGameV.getSprite(x + 1).setFacingRight(false);
		}
	}
	
	/* JNI oriented */
	public void playSounds() {
		if(getSoundOw() == Panel.JNI_TRUE) {
			mSounds.playSound(SoundPoolManager.SOUND_OW);
			//Log.e("Play-Sound", "OW");
		}
		if(getSoundPrize() == Panel.JNI_TRUE) {
			mSounds.playSound(SoundPoolManager.SOUND_PRIZE);
		}
		if(getSoundBoom() == Panel.JNI_TRUE) {
			mSounds.playSound(SoundPoolManager.SOUND_BOOM);
			//Log.e("Play-Sound","BOOM");
		}
		
		if(this.getSoundGoal() == Panel.JNI_TRUE) {
			mSounds.playSound(SoundPoolManager.SOUND_GOAL);
			//
		}
		if (this.getSoundEnter1() == Panel.JNI_TRUE) {
			mSounds.playSound(SoundPoolManager.SOUND_ENTER_1);
		}
		if (this.getSoundEnter2() == Panel.JNI_TRUE) {
			mSounds.playSound(SoundPoolManager.SOUND_ENTER_2);
		}
		if ( this.getSoundEnter3() == Panel.JNI_TRUE) {
			mSounds.playSound(SoundPoolManager.SOUND_ENTER_3);
		}
		if ( this.getSoundEnter4() == Panel.JNI_TRUE) {
			mSounds.playSound(SoundPoolManager.SOUND_ENTER_4);
		}
	}
	
	
	public void addMonstersJNI() {
		for (int i = mGameV.getMonsterOffset(); i <= mGameV.getMonsterNum()  ; i ++) {
			SpriteInfo temp = mGameV.getSprite(i);
			addMonster(temp.getMapPosX(), temp.getMapPosY(), temp.getAnimIndex());

		}
	}
	
	public void addPlatformsJNI() {
		if (mGameV.getPlatformNum() == -1) return;
		for (int i = mGameV.getPlatformOffset() ; i <=  mGameV.getPlatformNum() ; i ++) {
			SpriteInfo temp = mGameV.getSprite(i);
			addPlatform(temp.getMapPosX(), temp.getMapPosY());

		}
	}
	public void addChallengesJNI() {
		mGameV.resetChallengeIndex();
		while(!mGameV.isChallengeFinished()) {
			Challenge temp = mGameV.readNextChallenge();
			this.JNIaddChallenges(temp.getRings(), temp.getBubble1(), temp.getBubble2(), temp.getBubble3(), 
						temp.getInvader1(), temp.getInvader2(), temp.getInvader3(), 
						temp.getSpeed());
		}
	}
	
	public boolean isLanding() {
		boolean temp = false;
		if(JNIisLanding() == Panel.JNI_TRUE) {
			temp = true;
		}
		else {
			temp = false;
		}
		return temp;
	}
	
	public native void setLevelData( int [] a_map, int [] b_map,int width, int height);
	public native void setObjectsDisplay(int map_x, int map_y, int value);
	public native void setGuyData(int [] a, int [] b, int [] c, int [] d);
	public native void setInvader1Data(int [] l, int [] r);
	public native void setInvader2Data(int [] a, int [] b);
	public native void setFlyerData(int [] a, int [] b, int [] c, int [] d);
	public native void setMonsterData(int [] a, int [] b, int [] c, int [] d);
	public native void setExplosionData(int [] a, int [] b, int [] c, int [] d, int [] e, int [] f, int [] g, int [] h );
	public native void setMovingPlatformData(int []a);
	public native void inactivateMonster(int num);
	public native void setTileMapData( int [] a, int [] b, int [] c, int [] d);
	public native void addMonster(int map_x, int map_y, int animate_index);
	public native void addPlatform(int map_x, int map_y);
	public native void setGuyPosition(int x, int y, int scrollx, int scrolly, int keyB);
	public native void setScoreLives(int score, int lives);
    public native void setMonsterPreferences(int monsters, int collision);
    public native void setJNIAnimateOnly(int animate);
    public native void setScreenData(int screenH, int screenV);
	//public native void drawLevel(); // <-- remove me!!
	public native int getSoundBoom();
	public native int getSoundOw();
	public native int getSoundPrize();
	public native int getSoundGoal();
	public native int getSoundEnter1();
	public native int getSoundEnter2();
	public native int getSoundEnter3();
	public native int getSoundEnter4();
	public native int getEndLevel();
	public native int getGameDeath();
	public native int getScore();
	public native int getLives();
	public native void incrementJniScore(int num);
	public native int getSpriteX(int num);
	public native int getSpriteY(int num);
	public native int getSpriteFacingRight(int num);
	public native void JNIaddChallenges(int rings, int bubble1, int bubble2, int bubble3, int invader1, int invader2, int invader3, int speed);
	public native int setJNIScroll(int x, int y);
	public native void JNIfinishInit();
	public native int JNIisLanding();
	//opengl native methods
	public native void JNIinit();
	public native void JNIdraw();
	public native void JNIresize(int w, int h);
	public native void JNIsetVersion(int v);
	public native void JNIbuildLevel();
	public native void JNIblankScreen(int makeBlank);
	public native int JNIisAnimateOnly();
	public native void JNIscreenFlip(int f);
	static {
		
		
			System.loadLibrary("flyer");
		
	}
	
	
	
}
